import { type BrowserWindowConstructorOptions, nativeImage } from 'electron';
import { platform } from '@electron-toolkit/utils';
import { merge } from 'lodash-es';
import { join } from 'node:path';

const defaultConfig = (() => {
  const config: BrowserWindowConstructorOptions = {
    width: 350,
    height: 450,
    minWidth: 170,
    maxWidth: 500,
    minHeight: 450,
    maxHeight: 800,
    frame: false,
    resizable: false,
    show: true,
    autoHideMenuBar: true,
    titleBarStyle: platform.isMacOS ? 'hidden' : 'default',
    webPreferences: {
      // 同源策略
      webSecurity: false,
      nodeIntegration: false, //禁止独立js环境
      webviewTag: false, // webview
      // Chromium 的实验功能
      experimentalFeatures: false,
      // macos 滚动回弹模式
      scrollBounce: platform.isMacOS,
      // 开启时，preload预加载脚本的能力会受一定的限制
      sandbox: false,
      // 关闭内置拼写器
      spellcheck: false
      //additionalArguments: ['--disable-web-security', '--allow-file-access-from-files']
    }
  };

  let iconPath = '';
  if (platform.isWindows) {
    iconPath = join(__dirname, '../../resources/icons', 'icon.ico');
  } else if (!platform.isMacOS) {
    iconPath = join(__dirname, '../../resources/icons', 'icon.png');
  }
  if (iconPath) {
    config.icon = nativeImage.createFromPath(iconPath);
  }
  return config;
})();

function getBaseConfig(options: BrowserWindowConstructorOptions) {
  return merge({}, defaultConfig, options);
}

export { defaultConfig, getBaseConfig };
